﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 手动执行任务
    /// </summary>
    [CPPEI.WebApi.PostAction("ETL", "ManualVerifyData", "1.0")]
    public class ManualVerifyDataAction : BaseAction<ManualVerifyDataParams, ManualVerifyDataResult>
    {
        protected override ActionResult<ManualVerifyDataResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            ManualVerifyDataParams opParams)
        {
            Logger.DebugToTag(Library.LogTAG, string.Format("ManualVerifyData,TaskId:{0}", opParams.TaskId));

            SER.TaskService service = new SER.TaskService();
            ServiceResult<MOD.ManualDataVerifyDataResult> serResult = service.ManualDataVerify(opParams.TaskId, opParams.ErrorDataSettings);

            if (serResult.IsSuccess)
            {
                ManualVerifyDataResult result = new ManualVerifyDataResult();
                result.DataCount = serResult.Data.DataCount;
                result.CodeConvertFailureCount = serResult.Data.CodeConvertFailureCount;
                result.DataVerifyFailureCount = serResult.Data.DataVerifyFailureCount;
                result.InsertSuccessCount = serResult.Data.InsertSuccessCount;
                result.InsertFailureCount = serResult.Data.InsertFailureCount;

                return new ActionResult<ManualVerifyDataResult>(result);
            }
            else
            {
                return new ActionResult<ManualVerifyDataResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class ManualVerifyDataParams
    {
        [Parameter]
        public Guid TaskId { get; set; }

        /// <summary>
        /// 数据编号(_Id),是否有错误（true有错误，false无错误）,Message错误信息
        /// dataid,hasError,message
        /// </summary>
        [Parameter]
        public string ErrorDataSettings { get; set; }
    }

    public class ManualVerifyDataResult
    {
        public long DataCount { get; set; }

        public long DataVerifyFailureCount { get; set; }

        public long CodeConvertFailureCount { get; set; }

        public long InsertSuccessCount { get; set; }

        public long InsertFailureCount { get; set; }
    }
}
